package com.example.hobbyplanetserve.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * token工具类
 */
@Component
public class JwtUtils {
    //生成token
    public static String generateToken(String username,Long r_id,Long expire,String secretKey){
        Date now = new Date();
        Date expiration = new Date(now.getTime() + 1000 + expire);
        System.out.println("传进来generateToken的r_id为" + r_id);
        return Jwts.builder()
                .setHeaderParam("type","JWT")
                .setSubject(username)
                .setId(String.valueOf(r_id))
                .setExpiration(expiration)
                .signWith(SignatureAlgorithm.HS512,secretKey)
                .compact();
    }
    //解析token
    public static Claims getClaimsByToken(String token,String secretKey){
        return Jwts.parser()
                .setSigningKey(secretKey)
                .parseClaimsJws(token)
                .getBody();
    }
}
